var Users = {

    selected_row_id:null,
    objGrid:null,

    init:function () {
        var self = this;

        self.objGrid = $('#flex').flexigrid({
            url:$('#table-url').val(),
            //reload_after_add:true,
            reload_after_edit:true,
            dataType:'json',
            colModel:[
                {display:'ID', name:'id', width:20, sortable:false, align:'left', hide:true},
                {display:'First Name', name:'fname', width:100, sortable:false, align:'center', hide:false, editable:{form:'text', validate:'empty', defval:''}},
                {display:'Middle Name', name:'mname', width:100, sortable:false, align:'center', hide:false, editable:{form:'text', validate:'', defval:''}},
                {display:'Last Name', name:'lname', width:100, sortable:false, align:'center', hide:false, editable:{form:'text', validate:'empty', defval:''}},
                {display:'Username', name:'username', width:100, sortable:false, align:'center', hide:false, editable:{form:'text', validate:'empty', defval:''}},
                {display:'Level', name:'user_level', width:70, sortable:false, align:'center', hide:false, editable:{form:'select', validate:'', defval:'',options:user_level}},
                {display:'Status', name:'active', width:70, sortable:false, align:'center', hide:false, editable:{form:'select', validate:'', defval:'',options:active_list}},
                {display:'Branch', name:'bank_branch_id', width:100, sortable:false, align:'center', hide:false, editable:{form:'select', validate:'', defval:'',options:branch_list}}
            ],
            formFields:[
                {type:'buttom', name:'New', bclass:'add', onpress:self.handleGridEvent},
                {separator:true},
                {type:'buttom', name:'Save', bclass:'save', onpress:self.handleGridEvent},
                {separator:true},
                {type:'buttom', name:'Edit', bclass:'edit', onpress:self.handleGridEvent},
                {separator:true},
                {type:'buttom', name:'Cancel', bclass:'cancel', onpress:self.handleGridEvent},
                {separator:true},
                {type:'buttom', name:'Delete', bclass:'delete', onpress:self.handleGridEvent},
                {separator:true},
                {type:'buttom', name:'Reset Password', bclass:'enable', onpress:self.handleGridEvent}
            ],
            searchitems:[
                {display:'Username', name:'username', isdefault:true},
                {display:'First Name', name:'fname'},
                {display:'Last Name', name:'lname'},
                {display:'Middle Name', name:'mname'}
             ],
            checkboxSelection:true,
            editablegrid:{
                use:true,
                url:$('#table-editable-url').val(),
                callback:function (server_response) {
                    console.log(server_response)
                    if (server_response.code == 0) {
                        jLib.flexNotify('Data Status', server_response.msg, 'success');
                        //self.objGrid.flexReload();
                    }
                    else {
                        jLib.flexNotify('Data Status', server_response.msg, 'error');
                    }
                }
            },
            columnControl:false,
            sortname:"id",
            sortorder:"desc",
            usepager:true,
            useRp:true,
            rp:15,
            showTableToggleBtn:false,
            height:300,
            subgrid:{
                use:false
            }
        });

        $("#reset_modal form").submit(function(e) {
            e.preventDefault();
            self.doResetPassword();
        });
    },

    handleGridEvent:function (com, grid, json) {
        if (com == 'New') {
            Users.objGrid.flexBeginAdd();
        }
        else if (com == 'Edit') {
            var row = FlexObject.getSelectedRows(grid);
            Users.objGrid.flexBeginEdit(row[0]);
        }
        else if (com == 'Save') {
            Users.objGrid.flexSaveChanges();
        }
        else if (com == 'Cancel') {
            Users.objGrid.flexCancel();
        }
        else if (com == 'Delete') {
            if (FlexObject.rowSelectedCheck(Users.objGrid,grid)) {
                Users.delete_(grid);
            }
        }
        else if (com == 'Reset Password') {
            if (FlexObject.rowSelectedCheck(Users.objGrid,grid,1)) {
                var row = FlexObject.getSelectedRowIds(grid);
                Users.resetPass(row[0]);
            }
        }
    },

    delete_:function (grid) {
        var self = this;
        var url = $('#delete_url').val();
        jLib.do_delete(url, grid);
    },

    resetPass:function(id){
        $('#reset_modal #id').val(id);
        $('#reset_modal').modal({
            backdrop: 'static',
            show: true,
            keyboard: true
        });
    },

    /**
     * Triggers steps to make AJAX call to credit student account
     */
    doResetPassword: function() {
        var query = $("#reset_modal form").serialize();
        var url = $("#reset_modal #reset_pass_btn").attr("href");

       // Results.disableCreditForm(true);

        $.ajax({
            type: 'post',
            url: url,
            data: query,
            dataType: 'json',
            success: function(data) {
                if(data.code == 0){
                    $("#reset_modal").modal("hide");
                    alert(data.msg);
                }
                else{
                    alert(data.msg);
                }
                //Results.disableCreditForm(false);

            },
            error: function() {
                //Results.disableCreditForm(false);
            }
        });
    }
};

/* when the page is loaded */
$(document).ready(function () {
    Users.init();
});